<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
	include("includes/db_connect.php");
	$action = $_GET['action'];
	$h = $_GET['h'];
	$w = $_GET['w'];
	
	$seriesResult=mysql_query("SELECT s.id, s.tvdbseries_id, s.name, s.description, s.content_rating, s.first_aired, s.genre, s.network, s.network_image, s.rating, s.runtime, ci.subfolder_path, ci.filename FROM tbl_tvshow_series s LEFT OUTER JOIN tbl_cacheimage ci ON (s.tvdbseries_id = ci.link_id and ci.type_id=2 and ci.keyword='poster') ORDER BY name") or die(mysql_error());
	
	//variables for the top left info panel
	$leftTopLbl1 = 'TV Shows';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PS3 Theatre <?php echo $_SESSION['version'];?></title>
<script type="text/javascript" src="js/time.js"></script>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/animator.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/tvShows_ui.js"></script>

<script>
var lastPosterId = '';
var lastDivId = '';
var posterListIndex = 0;
var posterListLength = <?php echo mysql_num_rows($seriesResult);?>;

function showScanningWindow(action) {
		if(action != 'noScrape'){
			screenEnable(false);
		
		    showConfirmWindow('Scanning', '<CENTER>Scanning for file changes...</CENTER>', 100, 300, false);
			
			new Ajax.Request('scripts/tvShowScraper.php?action=scrapeAll&uId='+getUniqueCode(),
			  {
				method:'get',
				onSuccess: function(transport){
					closeConfirmWindow();
					var responseText = transport.responseText.strip();

					if(responseText == 'no changes'){
						//do nothing
					} else if(responseText == 'success'){
						location='tvshows.php?action=noScrape&uId='+getUniqueCode();
					}else if(responseText.substring(10,53) == 'Fatal error</b>:  Maximum execution time of'){
						alert('Scan was shutdown after a 5 minute time limit.  It didn\'t complete and will need to be ran again to finish the rest of the scan.');
					}else{
						alert('Error scanning for changes...');
					}
					
					screenEnable(true);
				},
				onFailure: function(){ screenEnable(true); alert('error scanning for changes...'); }
			  });
		  }
	}
	
	function selectPoster(divKey, posterKey, seriesId, runtime, firstAired, contentRating, networkImage, genre, rating)
	{
		$(posterKey).removeClassName('tvPoster');
		$(posterKey).addClassName('tvPosterHover');
		$(divKey).removeClassName('divTvPoster');
		$(divKey).addClassName('divTvPosterHover');
		
		if(lastPosterId != '' && lastPosterId != posterKey){
			unselectPoster(lastDivId, lastPosterId);
		}
		
		lastDivId = divKey;
		lastPosterId = posterKey;
		loadShowArt(seriesId);

		$('runtimeTD').innerHTML = runtime+' Mins';
		$('airedTD').innerHTML =  firstAired;
		if(contentRating != ''){
			$('contentRatingNetworkTD').innerHTML = '<img class="ratingsImg" src="images/flags/tv_ratings/'+contentRating+'.gif" width="75" height="75"/><img src="images/flags/tv_logos/'+networkImage+'.jpg" width="100" height="75"/>';
		}else{
			$('contentRatingNetworkTD').innerHTML = '<img class="ratingsImg" src="images/flags/tv_ratings/notrated.gif" width="75" height="75"/><img src="images/flags/tv_logos/'+networkImage+'.jpg" width="100" height="75"/>';
		}
		$('genreTD').innerHTML = genre;
		$('ratingTD').innerHTML = '<img src="images/rating'+rating+'.png" class="movieRating" width="200" height="42"/>';
	}

	function unselectPoster(divKey, posterKey)
	{
		$(posterKey).removeClassName('tvPosterHover');
		$(posterKey).addClassName('tvPoster');
		$(divKey).removeClassName('divTvPosterHover');
		$(divKey).addClassName('divTvPoster');
	}
	
	function loadShowArt(seriesId) {
		new Ajax.Request('scripts/tvShowImport.php?seriesId='+seriesId+'&vignette=Y&uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				if(json.vignetteSrc != ''){
					document.body.background=json.vignetteSrc;
				}else{
					document.body.background='images/movies-background.png';
				}
				toggleLoader();
			},
			onFailure: function(){ alert('error loading backdrop...');toggleLoader(); }
		  });
	 }
	 
	 function loadPosterArt(index, hrefObj, imgId, seriesId){
		new Ajax.Request('scripts/tvShowImport.php?seriesId='+seriesId+'&poster=Y&uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();

				if(json.posterSrc != ''){
					$('posterSpan'+index).innerHTML = '<img id="poster'+index+'" class="'+$(imgId).readAttribute('class')+'" src="'+json.posterSrc+'"/>';
				}else{
					$('posterSpan'+index).innerHTML = '<img id="poster'+index+'" class="tvPoster" src=""/>';
				}
			
				hrefObj.removeClassName('loadable-image');
				hrefObj.addClassName('loaded');
				toggleLoader();
			},
			onFailure: function(){ alert('error loading poster art...');toggleLoader(); }
		  });
	 
	 }
	 
	 function loadAllImages(){
	 	var imgDivs = $$('span.loadable-image');
		var i=0;
		while(i<imgDivs.length){
			loadPosterArt(imgDivs[i].readAttribute('index'),
						  imgDivs[i],
						  imgDivs[i].readAttribute('imgId'),
						  imgDivs[i].readAttribute('seriesId'));
			i++;
		}
	 }
	 
</script>
<link href="css/main.css" rel="stylesheet" type="text/css">
<meta http-equiv="Pragma" content="no-cache"> 
</head>
<body bgcolor="#000000" style="overflow:hidden;" onload="clock();showScanningWindow('<?php echo $action;?>');loadAllImages();">
<div class="wrapper" >

<!-- left info panel -->
<?php include('/includes/topLeftInfoPanel.php'); ?>

<!-- right info panel -->
<?php include('/includes/topRightInfoPanel.php'); ?>

<!-- series info panel-->
<?php if(mysql_num_rows($seriesResult) > 0){ ?>
<div style="position:absolute; top:100px; left:1440px; overflow:hidden; z-index:10; width:345px;">
<table width="100%">
<tr>
<td class="tvDescHeader" width="135">
Runtime
</td>
<td id="runtimeTD" class="tvDescText">
</td>
</tr>
<tr class="tvDescSpacer">
<td colspan="2">
</td>
</tr>
<tr>
<td class="tvDescHeader">
Aired
</td>
<td id="airedTD" class="tvDescText">
</td>
</tr>
<tr class="tvDescSpacer">
<td colspan="2">
</td>
</tr>
<tr>
<td id="contentRatingNetworkTD" colspan="2" align="center">
</td>
</tr>
<tr class="tvDescSpacer">
<td colspan="2">
</td>
</tr>
<tr>
<td id="ratingTD" colspan="2" align="center">
</td>
</tr>
<tr class="tvDescSpacer">
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" id="genreTD" class="tvDescText">
</td>
</tr>
</table>
</div>
<?php }else{ ?>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<center>
<span class="mainLinkOver">
No TV Shows in system.
</span>
</center>
<?php } ?>


<!-- poster scroll panel-->
<div id="posterPanel" style="position:absolute; top:610px; width:100%; height:386px; overflow:hidden; vertical-align:top;">
<table id="posterTable" height="386" style="margin-left:765px;">
<tr style="vertical-align:bottom;">
<?php
	$i = 1;
	while ($row = mysql_fetch_array($seriesResult)) {
	$genreString = str_replace("|","<br/>",substr($row['genre'],1,strlen($row['genre'])-2));
	
	if($i == 1){
		$initialTvdbSeriesId = $row['tvdbseries_id'];
		$initialIndex = $i;
		$initialTotalNum = mysql_num_rows($seriesResult);
		$initialRuntime = $row['runtime'];
		$initialFirstAired = $row['first_aired'];
		$initialContentRating = $row['content_rating'];
		$initialNetworkImage = $row['network_image'];
		$intialGenre = $genreString;
		$initialRating = floor($row['rating']/2);
	}
	
	if($row['filename'] != ''){
		$loaderClassName = 'loaded';
		$posterSrc = $_SESSION['cacheHttpDir'].$row['subfolder_path'].$row['filename'];
	}else{
		$loaderClassName = 'loadable-image';
		$posterSrc = 'images/ajax-loadertv.gif';	
	}
?>
<td height="382">
<div id="divPoster<?php echo $i-1;?>" class="divTvPoster">
<span id="posterSpan<?php echo $i-1;?>" class="<?php echo $loaderClassName;?>" index="<?php echo $i-1;?>" imgId="poster<?php echo $i-1;?>" seriesId="<?php echo $row['tvdbseries_id'];?>" runtime="<?php echo $row['runtime'];?>" firstAired="<?php echo $row['first_aired'];?>" contentRating="<?php echo $row['content_rating'];?>" networkImage="<?php echo $row['network_image'];?>" genre="<?php echo $genreString;?>" rating="<?php echo floor($row['rating']/2);?>" link="tvShowsSeasons.php?seriesId=<?php echo $row['id'];?>&seriesName=<?php echo $row['name'];?>&w=<?php echo $w;?>&h=<?php echo $h;?>&uId=<?php echo time();?>">
<img id="poster<?php echo $i-1;?>" class="tvPoster" src="<?php echo $posterSrc;?>"/>
</span>
</div>
</td>
<?php
$i++;
}
?>
</tr>
</table>
</div>

<!-- scrollbar back -->
<div style="position:absolute; left:50px; top:1000px; z-index:6;">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="images/scrollbarback_horizontal_left.png" height="17" width="22"/></td>
    <td style="width:1656px; background:url(images/scrollbarback_horizontal_middle.png) repeat-x;"></td>
    <td><img src="images/scrollbarback_horizontal_right.png" height="17" width="22"/></td>
  </tr>
</table>
</div>

<!-- scrollbar nub -->
<div id="scrollNub" style="position:absolute; left:70px; top:1002px; z-index:6;" class="scrollNubHorizontal">
</div>
</div>

<!-- Modal Window Code -->
<?php include('/includes/modalWindow.php'); ?>

<!-- script to select the first series -->
<script>
	selectPoster('divPoster<?php echo "0', 'poster0',".$initialTvdbSeriesId.",".$initialRuntime.",'".$initialFirstAired."','".$initialContentRating."','".$initialNetworkImage."','".$intialGenre."',".$initialRating;?>);
</script>
</body>
</html>
